#include"LandOwner.h"

LandOwner::LandOwner()
{
    SetNickname("默认地主");
    SetSex("保密");
    SetGold(1000);
    SetExp(0);
    // cout << "LandOwner()" << endl;
    InitCards();
}
LandOwner::LandOwner(string name) : m_Nickname (name)
{
    SetSex("保密");
    SetGold(1000);
    SetExp(0);
    // cout << "LandOwner(string name)" << endl;
    InitCards();
}
LandOwner::LandOwner(string name, string sex, int gold, long exp) : 
                    m_Nickname(name),m_Sex(sex),m_Gold(gold),m_Exp(exp)
{
    // cout << "LandOwner(string name, string sex, int gold, long exp)" << endl;
    InitCards();
}
void LandOwner::ShouInfo()
{
    cout << "姓名：" << GetNickname() << endl;
    cout << "性别：" << GetSex() << endl;
    cout << "金币：" << GetGold() << endl;
    cout << "经验：" << GetExp() << endl;
}

void LandOwner::InitCards()//初始化packCards,surplusCards,surrCards
{
    //生成默认的一副牌
    for (int i = 0; i < 54;i++)
    {
        packCards.push_back(i + 1);
        surplusCards.push_back(packCards.at(i));
    }
    currCards.clear();
    // ShowCards(packCards);

}

// void LandOwner::ShowCards1(const vector<int>& cards)
// {
//     //C写法
//     // for (int i = 0; i < cards.size();i++)
//     //     cout << cards[i] << ",";
//     // cout << endl;
    
//     //C++写法使用迭代器,iter是指针
//     // for (vector<int>::const_iterator iter = cards.begin(); iter != cards.end(); iter++)
//     //     cout << *iter << ",";
//     // cout << endl;
    
//     //C++11的类型推断
//     // for (auto iter = cards.begin(); iter != cards.end(); iter++)
//     //     cout << *iter << ",";
//     // cout << endl;
//     //不太清楚，再看看 
//     // for (auto card : cards)
//     //     cout << card << ",";
//     // cout << endl;

//     //使用算法的方法，将容器的内容复制到cout绑定的迭代器中
//     //#include<iterator>    #include<algorithm> 算法
//     copy(cards.cbegin(), cards.cend(), ostream_iterator<int>(cout, ","));
//     cout << endl;
// }

void LandOwner::TouchCards(int cardCount)
{
    //随机从剩余牌surplusCards中选一张牌并删除，添加到currCards中
    srand(time(NULL));//时间种子
    for (int i = 0; i < cardCount;i++)
    {
        int randIndex = rand() % 54;
        //判断随机生成的牌是否在剩余牌中
        if(isContains(packCards[randIndex]))
        {
            currCards.push_back(packCards[randIndex]);//将摸的牌放入手牌数组
            deleteCard(surplusCards, packCards[randIndex]);//在剩余牌中删除这张牌
        }
        else
            i--;//重新摸牌
    }
    cout << "地主手牌如下：" << endl;
    ShowCards(currCards);
    cout << "剩余牌面如下：" << endl;
    ShowCards(surplusCards);
}
bool LandOwner::isContains(int cardNum)
{
    //普通写法
    // for (int i = 0; i < surplusCards.size();i++)
    //     if(surplusCards[i]==cardNum);
    //         return true;
    // return false;
    //算法查找//也不知道哪出的问题
    vector<int>::iterator iter = find(surplusCards.begin(), surplusCards.end(), cardNum);
    // if(iter == surplusCards.end())
    //     return false;
    // return true;
    return iter != surplusCards.end();
}

void LandOwner::deleteCard(vector<int> &cardVec, int card)
{
    // 普通写法
    // for (auto iter = cardVec.begin(); iter != cardVec.end();)
    // {
    //     if(*iter==card)//如果找到就删除元素
    //         iter = cardVec.erase(iter);//这里的返回值指向已删除已删除元素的下一个
    //     else
    //         ++iter;
    // }
    //使用算法删除
    auto iter = find(cardVec.begin(), cardVec.end(), card);
    if(iter != cardVec.end())
        cardVec.erase(iter);
}

string LandOwner::getColor(int card)
{
    if(card==53)
        return "小王(Black Joker)";
    if(card==54)
        return "大王(Red Joker)";
    string colors[] = {"黑桃", "红心", "方片", "梅花"};
    return colors[(card - 1) / 13];//card是牌面，不是下标
}
string LandOwner::getValue(int card)
{
    if(card==53)
        return "Black Joker";
    if(card==54)
        return "Red Joker";
    string values[] = {"A", "2", "3", "4", "5", "6", "7",
                         "8", "9", "10", "J", "Q", "K"};
    return values[(card - 1) % 13];
}
void LandOwner::ShowCards(const vector<int>& cards)
{
    for(auto card:cards)
        cout << getColor(card) << getValue(card) << "  ";
    cout<<endl;
}

LandOwner::~LandOwner()
{
    // cout << GetNickname() << "被释放" << endl;
    ;
}